/**
 * ======================================================
 * sendprops.cpp
 *
 * Send prop retrieval
 * ======================================================
 *
 * The MIT License
 * 
 * Copyright (c) 2008 Matthew Maravillas
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Version: $Id$
 */

#include "sendprops.h"
#include "util.h"

void *SendProps::GetValue(void *base, int propOffset)
{
	if (propOffset <= 0)
    {
        META_LOG(g_PLAPI, "Send prop offset was invalid: %d", propOffset);

		return NULL;
    }

	return reinterpret_cast<void*>((char*)base + propOffset);
}

int SendProps::GetOffset(const char *className, const char *propName)
{
    DEBUG1("Getting server class %s", className);

    ServerClass *serverClass = FindServerClass(className);

    if (serverClass == NULL)
    {
        DEBUG1("Server class %s could not be found", className);
    }
    else
    {
        DEBUG1("Getting send prop %s", propName);

        SendPropOffset propOffset;

        if (!SendProps::FindProp(serverClass->m_pTable, propName, &propOffset, 0))
        {
            META_LOG(g_PLAPI, "Send prop %s could not be found", propName);
        }
        else
        {
            DEBUG2("Send prop %s is at offset %d", propOffset.prop->GetName(), propOffset.offset);

            return propOffset.offset;
        }
    }

    return -1;
}


ServerClass *SendProps::FindServerClass(const char *name)
{
	ServerClass *serverClass = mServer->GetAllServerClasses();

	while (serverClass)
	{
		if (strcmp(serverClass->m_pNetworkName, name) == 0)
		{
			return serverClass;
		}

		serverClass = serverClass->m_pNext;
	}

	return NULL;
}

bool SendProps::FindProp(SendTable *sendTable, const char *propName, SendPropOffset *propOffset, unsigned int offset)
{
	int propCount = sendTable->GetNumProps();

	for (int i = 0; i < propCount; i++)
	{
		SendProp *prop = sendTable->GetProp(i);
		const char *currentName = prop->GetName();

		if (currentName && 
            strcmp(propName, currentName) == 0)
		{
			propOffset->prop = prop;
			propOffset->offset = offset + propOffset->prop->GetOffset();
			return true;
		}

		if (prop->GetDataTable())
		{
			if (FindProp(prop->GetDataTable(), propName, propOffset, offset + prop->GetOffset()))
			{
				return true;
			}
		}
	}

	return false;
}

